/** \file    Reduce.cpp
  * \brief   Removes inactive segments from segmentation.
  * \date    2001
  * \author  Fedor Chelnokov
  * \version $Header: /home/cscom1r4/cvsroot/cs/segm/src/Reduce.cpp,v 1.1 2004/05/20 09:11:46 cscom1r4 Exp $
  */

#include "segm/Segmentation.h"

void Segmentation::reduce()
{
	IntArr newSegmentIndeces( count(), -1 );

	segmentIvector newSegments;
	size_t i;
	for (i = 0; i != count(); ++i) {
		if (m_segmentIvector[i].active) {
			newSegmentIndeces[i] = 
				newSegments.insert(m_segmentIvector[i]);
		}
	}
	m_segmentIvector.swap( newSegments );

	IntIntMap::iterator j;
	for( i = 0; i != m_segmentIvector.size(); ++i ) {
		for( j = m_segmentIvector[i].neighbours.begin(); j != m_segmentIvector[i].neighbours.end(); ) {
			if( newSegmentIndeces[j->second] == -1 ) {
				m_segmentIvector[i].neighbours.erase( j++ );
			} else {
				j->second = newSegmentIndeces[j->second];
				++j;
			}
		}
	}
	YATL::Arr2i::iterator k;
	for( k = m_segmentCoordMap.begin(); k != m_segmentCoordMap.end(); ++k ) {
		if( *k == -1 ) continue;
		if( newSegmentIndeces[*k] == -1 ) {
			*k = -1;
		} else {
			*k = newSegmentIndeces[*k];
		}
	}
}
